package com.UI
{
	import com.*;
	import com.Engine.*;
	import com.Packet.*;
	import com.UI.*;
	
	import flash.display.*;
	import flash.display.BitmapData;
	import flash.events.*;
	import flash.external.*;
	import flash.filters.*;
	import flash.geom.*;
	import flash.net.*;
	import flash.system.*;
	import flash.text.*;
	import flash.ui.*;
	import flash.utils.*;
	import flash.utils.ByteArray;
	import flash.utils.getQualifiedClassName;
	
	import org.aswing.*;
	import org.aswing.border.*;
	import org.aswing.border.EmptyBorder;
	import org.aswing.colorchooser.*;
	import org.aswing.dnd.*;
	import org.aswing.event.*;
	import org.aswing.ext.*;
	import org.aswing.geom.*;
	import org.aswing.graphics.*;
	import org.aswing.tree.AbstractLayoutCache;
	
	public class PanelSkill extends JPanel
	{
		public var icon_id:uint = 0;
		public var index:uint = 0;
		public var userObject:Object = null;
		
		//members define
		public var panelImage:JPanel;
		private var _item:Object = null;
		public function PanelSkill()
		{
			//component creation
			setSize(new IntDimension(43, 43));
			//setPreferredSize(new IntDimension(43, 43));
			var layout0:EmptyLayout = new EmptyLayout();
			setLayout(layout0);
			
			panelImage = new JPanel();
			panelImage.setLocation(new IntPoint(2, 2));
			panelImage.setSize(new IntDimension(40, 40));
			
			//component layoution
			append(panelImage);
			
			addEventListener(MouseEvent.ROLL_OVER, onRollOver);
			addEventListener(MouseEvent.ROLL_OUT, onRollOut);
			/*if(img_background == null || img_background_selected == null)
			{
				img_background = new AssetBackground(Client.getUIElement("img_skill_bg") as DisplayObject);
				img_background_selected = new AssetBackground(Client.getUIElement("img_skill_bg_selected") as DisplayObject);
			}
			setBackgroundDecorator(img_background);*/
			setBackgroundDecorator(new AssetBackground(Client.getUIElement("img_skill_bg") as DisplayObject));
			
			//var matrix : Array = [0.3086, 0.6094, 0.0820, 0, 0, 0.3086, 0.6094, 0.0820, 0, 0, 0.3086, 0.6094, 0.0820, 0, 0, 0, 0, 0, 1, 0];
			//this.filters = [new ColorMatrixFilter([0.3086, 0.6094, 0.0820, 0, 0, 0.3086, 0.6094, 0.0820, 0, 0, 0.3086, 0.6094, 0.0820, 0, 0, 0, 0, 0, 1, 0])];
			//this.alpha = 0.7;
			
		}
		public function clear():void
		{
			setBackgroundDecorator(null);
			panelImage.setBackgroundDecorator(null);
		}
		private var _filtersGray:Array = [new ColorMatrixFilter([0.3086, 0.6094, 0.0820, 0, 0, 0.3086, 0.6094, 0.0820, 0, 0, 0.3086, 0.6094, 0.0820, 0, 0, 0, 0, 0, 1, 0])];
		private var _tooltip:ToolTipSkill = new ToolTipSkill();
		public var checkBattleEnableUse:Boolean = false;
		public var battleEnableUse:Boolean = true;
		public function set item(val:Object):void
		{
			_item = val;
			_tooltip.setTargetComponent(null);
			battleEnableUse = true;
			//panelImage.setBackgroundDecorator(null);
			//panelImage.setEnabled(false);
			panelImage.filters = null;
			if(_item == null)
			{
				icon_id = 0;
				panelImage.setBackgroundDecorator(null);
				return;
			}
			
			var objSkillCSV:Object = Client.getInstance().GetSkillCSV(_item.csv_id);
			if(objSkillCSV == null)
			{
				icon_id = 0;
				panelImage.setBackgroundDecorator(null);
				return;
			}
			if(icon_id != objSkillCSV.icon_id)
			{
				icon_id = objSkillCSV.icon_id;
				panelImage.setBackgroundDecorator(new AssetBackground(Client.getSkillElement("img_skill_"+objSkillCSV.icon_id) as DisplayObject));
			}
			
			_tooltip.setItem(_item);
			_tooltip.setTargetComponent(this);
			//panelImage.setEnabled(item.m_nLevel != 0);
			if(checkBattleEnableUse)
			{
				if(PanelMain.getInstance().isInBattle && SceneBattle.objSelf != null)
				{
					if(SceneBattle.objSelf.hp <= SceneBattle.objSelf.hp_max * _item.m_nConsumeHPRate/100)
					{
						panelImage.filters = _filtersGray;
						battleEnableUse = false;
					}
				}
			}
			panelImage.setSize(new IntDimension(this.width-3, this.height-3));
			pack();
			this.paintImmediately();
		}

		public function get item():Object
		{
			return _item;
		}
		
		private var filterArray:Array = [new GlowFilter(0xFFFF00, 1, 2, 2, 5)];
		private function onRollOver(event:MouseEvent):void
		{
			
			if(filters != filterArray)
				filters = filterArray;
			//if(item.m_nLevel == 0)
			//	this.filters = filterArray + _filtersGray;
			
			if(this.parent is Container)
				(this.parent as Container).bringToTop(this);
		}
		
		private function onRollOut(event:MouseEvent):void
		{
			filters = null;
			//if(item.m_nLevel == 0)
			//	this.filters = _filtersGray;
		}
		
		/*private var img_background:AssetBackground = null;
		private var img_background_selected:AssetBackground = null;
		private function onRollOver(event:MouseEvent):void
		{
			if(getBackgroundDecorator() != img_background_selected)
				setBackgroundDecorator(img_background_selected);
		}
		
		private function onRollOut(event:MouseEvent):void
		{
			if(getBackgroundDecorator() != img_background)
				setBackgroundDecorator(img_background);
		}*/
	}
}